<?php

namespace App\Models;

use App\ModelFilters\AdminRolesFilter;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\AdminRole
 *
 * @property int $id 主键
 * @property string $identifier 角色编号
 * @property string $name 角色名称
 * @property int $number 该角色拥有管理员数量限制
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole whereIdentifier($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole whereNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole whereUpdatedAt($value)
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole filter($input = array(), $filter = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole paginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole simplePaginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminRole whereLike($column, $value, $boolean = 'and')
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Admin[] $admins
 */
class AdminRole extends Model
{
    const SUPER_IDENTIFIER = 'v001'; //超级管理员唯一编号

    use Filterable;

    /**
     * 定义默认模型过滤器
     *
     * @return string
     */
    public function modelFilter()
    {
        return $this->provideFilter(AdminRolesFilter::class);
    }

    /**
     * 定义不允许自动填充的字段
     * @var array
     */
    protected $guarded = [
        'id',
    ];

    /**
     * 关联管理员model
     */
    public function admins()
    {
        return $this->hasMany('App\Models\Admin', 'role', 'id');
    }

    /**
     * 获取角色列表
     *
     * @param int $page_size
     * @return array
     */
    public static function getList($params, $page_size = 0)
    {
        $order_field = 'created_at';
        $order_sort = 'desc';
        if (0 === $page_size) {
            $list = self::filter($params)
                ->withCount('admins')
                ->orderBy($order_field,$order_sort)
                ->get();
            $total = (int)$list->count();
            $result = [
                'list' => $list,
                'total' => $total,
            ];
        } else {
            $list = self::filter($params)
                ->withCount('admins')
                ->orderBy($order_field, $order_sort)
                ->paginate($page_size);
            $temp = $list->toArray();
            $result = [
                'list' => $temp['data'],
                'total' => $temp['total'],
            ];
        }
        return $result;
    }
}
